package com.lmy.androidcustomview.touch

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.View
import com.lmy.androidcustomview.R
import com.lmy.androidcustomview.dp

/**
 * @author
 * @功能:多点触摸的View 触摸拖拽图片的位置 接力形
 * @Creat 2/14/21 1:44 PM
 * @Compony 465008238@qq.com
 */

class MultiTouchView2(context: Context, attrs: AttributeSet) : View(context, attrs) {
    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val bitmap = getAvatar(200.dp.toInt())

    //图像的初始偏移
    private var originalOffsetX = 0f
    private var originalOffsetY = 0f

    //绘制的时候真正的偏移量
    private var offsetx = 0f
    private var offsety = 0f

    //记录ACTION_DOWN事件按下的位置信息
    private var downx = 0f
    private var downy = 0f
    private var trackingPointID = 0

    override fun draw(canvas: Canvas) {
        super.draw(canvas)
        canvas.drawBitmap(bitmap, offsetx, offsety, paint)
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        when (event.actionMasked) {
            MotionEvent.ACTION_POINTER_DOWN -> {
                //event.actionIndex获取当前按下手指的Pointer的Index然后获取Pointer的ID并记录
                trackingPointID = event.getPointerId(event.actionIndex)
                //设置初始偏移量 每次DOWN事件时会更新初始偏移量
                //这里的downx坐标 不能用原来手指的按下坐标 需要换到最新的
                downx = event.getX(event.actionIndex)
                downy = event.getY(event.actionIndex)
                originalOffsetX = offsetx
                originalOffsetY = offsety

            }

            MotionEvent.ACTION_POINTER_UP -> {
                val actionIndex = event.actionIndex
                val pointID = event.findPointerIndex(actionIndex)
                //如果松开的手指 和最后点击的那个手指id一样话
                if (pointID == trackingPointID) {
                    //如果当前松开的Index为最后一个Index需要-2 因为这个时候这根手指已经松开了 不存在了所以需要-2
                    val newIndex =
                        if (actionIndex == event.pointerCount - 1) {
                            event.pointerCount - 2
                        } else {
                            event.pointerCount - 1
                        }

                    trackingPointID = event.getPointerId(newIndex)
                    //设置初始偏移量 每次DOWN事件时会更新初始偏移量
                    //这里的downx坐标 不能用原来手指的按下坐标 需要换到最新的
                    downx = event.getX(newIndex)
                    downy = event.getY(newIndex)
                    originalOffsetX = offsetx
                    originalOffsetY = offsety

                }

            }
            MotionEvent.ACTION_DOWN -> {
                trackingPointID = event.getPointerId(0)
                //设置初始偏移量 每次DOWN事件时会更新初始偏移量
                originalOffsetX = offsetx
                originalOffsetY = offsety
                downx = event.x
                downy = event.y
            }
            MotionEvent.ACTION_MOVE -> {
                //真正的偏移量=当前移动的位置-按下位置+初始偏移量

                //findPointerIndex根据PointerID获取 Pointer的Index然后使用getX获取坐标信息
                offsetx =
                    event.getX(event.findPointerIndex(trackingPointID)) - downx + originalOffsetX
                offsety =
                    event.getY(event.findPointerIndex(trackingPointID)) - downy + originalOffsetY
                invalidate()
            }
        }
        return true
    }

    /**
     * 限制图片的大小
     */
    private fun getAvatar(width: Int): Bitmap {
        val options = BitmapFactory.Options()
        options.inJustDecodeBounds = true
        BitmapFactory.decodeResource(
            resources,
            R.drawable.ic_head, options
        )
        options.inJustDecodeBounds = false
        options.inDensity = options.outWidth
        options.inTargetDensity = width
        return BitmapFactory.decodeResource(
            resources,
            R.drawable.ic_head, options
        )
    }
}

